package fresher.servletcontroller;

import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import hapax.TemplateException;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author namlq2
 */
public class ErrorServletController {

    public static void showError(HttpServletRequest request,
            HttpServletResponse response, String message) throws IOException {
        try {
            //Load template file
            TemplateLoader templateLoader = TemplateResourceLoader.create("fresher/");
            Template template = templateLoader.getTemplate("/view/error.xtm");
            TemplateDataDictionary dictionary = TemplateDictionary.create();

            dictionary.setVariable("message", message);
            //Render output and output
            String content = template.renderToString(dictionary);
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(content);
        } catch (TemplateException ex) {
            Logger.getLogger(ErrorServletController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
